package com.baozun.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.baozun.Exceptions.Number2StringException;

/**
 * 字符串工具类
 * 
 * @Title:
 * @Company:宝尊电子商务有限公司
 * @Author:万世极缘
 * @Since:2015-1-6
 * @Copyright:Copyright (c) 2015
 * @ModifyDate:2015-1-6
 * @Version:1.1.0
 */
public class StringUtil {

    public static boolean isContain(String[] strArray, String str) {
        boolean isContain = false;
        for (String strSingle : strArray) {
            if (str.indexOf(strSingle) > -1) {
                isContain = true;
                break;
            }
        }
        return isContain;
    }

    /**
     * 
     * @param intStr
     * @param defaultInt
     * @return
     */
    public static int getIntFromStr(String intStr, int defaultInt) {

        if (StringUtils.isNotEmpty(intStr)) {
            defaultInt = Integer.parseInt(intStr);
        }
        return defaultInt;
    }

    public static List<String> absPattern(String target, String regex) {
        List<String> result = null;
        if (StringUtils.isNotEmpty(target)) {
            try {
                Pattern pattern = Pattern.compile(regex);
                Matcher matcher = pattern.matcher(target);
                while (matcher.find()) {
                    if (result == null) {
                        result = new ArrayList<String>();
                    }
                    result.add(matcher.group());
                }
            } catch (Throwable e) {
                e.printStackTrace();
                return null;
            }
        }
        return result;
    }

    /**
     * 将文本中的<>替换掉
     * 
     * @param content
     * @return
     * @Description:
     */
    public static String content2Html(String content) {
        content = content.replaceAll("&", "&amp;");
        content = content.replaceAll("<", "&lt;");
        content = content.replaceAll(">", "&gt;");
        content = content.replaceAll("\"", "&quot;");
        return content;
    }

    /**
     * 字符串的输出
     * 
     * @param value
     * @return
     * @Description:
     */
    public static String param2Html(String value) {
        if (StringUtils.isEmpty(value)) {
            value = "";
        }
        return value;
    }

    /**
     * 日期类型的输出显示
     * 
     * @param date
     * @return
     * @Description:
     */
    public static String param2Html(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String dateStr = sdf.format(date);
        return dateStr;
    }

    /**
     * 数字显示类型
     * @param num
     * @param pattern
     * @return
     * @Description:
     */
    public static String param2Html(Object num, String pattern) {
        DecimalFormat df = new DecimalFormat(pattern);
        boolean isNumber = false;
        if (num == null || num instanceof Double || num instanceof Long || num instanceof Float || num instanceof Integer) {
            isNumber = true;
        }
        if (!isNumber) {
            try {

                throw new Number2StringException("该数不是数字类型！请检查!");
            } catch (Number2StringException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return num == null ? "" : df.format(num);

    }
}
